<?php

class Wiki_Entry_Controller extends Base_Controller
{

    public function action_detail($id = null)
    {
        $View = View::make("wiki::entry.detail");

        $Entry = Entry::find($id);
        $View->entry = $Entry;

        return $View;
    }

    public function action_edit($id = null)
    {
        $View = View::make("wiki::entry.edit");

        $Entry = Entry::find($id);
        if (isset($_POST["save"])) {
            $input = Input::all();
            $rules = array(
                'title' => 'required',
                'content' => 'required',
            );
            $validation = Validator::make($input, $rules);
            if ($validation->fails()) {
                $View->error = true;
            } else {
                $Entry->fill(array(
                    'title' => $input["title"],
                    'content' => $input["content"],
                ));

                $categories = array();
                if (!empty($input["categories"]))
                    foreach ($input["categories"] as $value) {
                        if (!is_numeric($value)) {
                            $Category = Category::where('language_id', '=', Language::getActive())->where('category', '=', $value)->get();
                            $Category = current($Category);
                            if (empty($Category)) {
                                $Category = new Category();
                                $Category->fill(array(
                                    'category' => $value,
                                    'language_id' => Language::getActive(),
                                ));
                                $Category->save();
                            }
                            $value = $Category->id;
                        }
                        $categories[] = $value;
                    }

                $tags = array();
                if (!empty($input["tags"]))
                    foreach ($input["tags"] as $value) {
                        if (!is_numeric($value)) {
                            $Tag = Tag::where('language_id', '=', Language::getActive())->where('tag', '=', $value)->get();
                            $Tag = current($Tag);
                            if (empty($Tag)) {
                                $Tag = new Tag();
                                $Tag->fill(array(
                                    'tag' => $value,
                                    'language_id' => Language::getActive(),
                                ));
                                $Tag->save();
                            }
                            $value = $Tag->id;
                        }

                        $tags[] = $value;
                    }

                $Entry->categories()->sync($categories);
                $Entry->tags()->sync($tags);
                $Entry->save();
                $View->success = true;
            }
        }
        $View->entry = $Entry;

        $entryTags = $Entry->tags()->get();
        $assignedTags = array();
        foreach ($entryTags as $entryTag) {
            $assignedTags[] = $entryTag->id;
        }

        $tags = Tag::where('language_id', '=', Language::getActive())->get();
        foreach ($tags as &$Tag) {
            if (in_array($Tag->id, $assignedTags)) {
                $Tag->selected = "selected='selected'";
            }
        }
        $View->tags = $tags;

        $entryCategories = $Entry->categories()->get();
        $assignedCategories = array();
        foreach ($entryCategories as $entryCategory) {
            $assignedCategories[] = $entryCategory->id;
        }

        $categories = Category::where('language_id', '=', Language::getActive())->where('parent_wiki_category_id', '=', 0)->order_by('category', 'ASC')->get();
        foreach ($categories as &$Category) {
            if (in_array($Category->id, $assignedCategories)) {
                $Category->selected = "selected='selected'";
            }
        }
        $View->categories = $categories;

        return $View;
    }

}